package model

import (
	"gin/common"
	Mysql "gin/database"
	"github.com/Gre-Z/common/jtime"
	"strings"
)

type Play struct {
	Id           uint64           `json:"id" gorm:"column:id;primaryKey;type:int;autoIncrement"`
	Name         string           `json:"name" binding:"max=255" gorm:"column:name"`
	Description  string           `json:"description" binding:"max=512" gorm:"column:description"`
	StoryId      uint64           `json:"story_id" binding:"required" gorm:"column:story_id;type:int"`
	VideoMediaId uint64           `json:"video_id" gorm:"column:video_id;type:int"`
	Sort         uint8            `json:"sort" binding:"max=65534" gorm:"column:sort;type:int"`
	SceneId      uint64           `json:"scene_id" gorm:"column:scene_id;type:int"`
	Created      jtime.TstampTime `json:"created" gorm:"column:created;autoCreateTime"`
	Updated      jtime.TstampTime `json:"updated" gorm:"column:updated;autoUpdateTime"`
	Scene        Scene            `json:"scene" gorm:"foreignKey:id;references:scene_id"`
	VideoMedia   Media            `json:"video" gorm:"foreignKey:id;references:video_id"`
}

// 设置表名
func (Play) TableName() string {
	return "play"
}

func (this *Play) Insert() (id uint64, err error) {
	result := Mysql.DB.Omit("Scene").Create(&this)
	id = this.Id
	if result.Error != nil {
		err = result.Error
		return
	}
	return
}

func (this *Play) Find(Param common.SelectQueryParam) (resData Play, err error) {

	dictOne := new(Play)
	db := Mysql.DB

	// 组装过滤条件
	if Param.Filter.Filter != nil {
		for key, val := range Param.Filter.Filter {
			vals := val.(map[string]interface{})
			if strings.Contains(key, "bw_") {
				db = db.Where(vals["condition"], vals["val1"], vals["val2"])
			} else {
				db = db.Where(vals["condition"], vals["val"])
			}
		}
	} else {
		db = db.Where("?", "1=1")
	}

	// 应用分页
	db = db.Select(Param.Fields)

	result := db.First(&dictOne)
	if result.Error != nil {
		err = result.Error
	}

	return *dictOne, err
}

func (this *Play) Select(Param *common.SelectQueryParam) (total int64, resData []Play, err error) {

	models := make([]Play, 0)
	db := Mysql.DB

	// 组装过滤条件
	if Param.Filter.Filter != nil {
		for key, val := range Param.Filter.Filter {
			vals := val.(map[string]interface{})
			if strings.Contains(key, "bw_") {
				db = db.Where(vals["condition"], vals["val1"], vals["val2"])
			} else {
				db = db.Where(vals["condition"], vals["val"])
			}
		}
	} else {
		db = db.Where("?", "1=1")
	}

	// 获取总条数
	db.Model(&Play{}).Count(&total)

	// 查找所有数据

	// 应用分页
	db = db.Select(Param.Fields).Limit(Param.Limit).Offset(Param.Offset)

	// 应用排序参数
	if Param.Order != "" {
		db = db.Order(Param.Order)
	}

	result := db.Find(&models)
	if result.Error != nil {
		err = result.Error
		return
	}

	return total, models, err
}

func (this *Play) FindById(id uint64) (resData Play, err error) {

	db := Mysql.DB

	db = db.Where("id = ?", id)

	// 应用分页
	db = db.Select("*")

	result := db.Preload("Scene").Preload("Scene.RoleGroups").First(&resData)
	if result.Error != nil {
		err = result.Error
		return resData, err
	}

	return resData, err
}

// 更新dict
func (this *Play) Update(media Play) (Play, error) {
	db := Mysql.DB
	result := db.Updates(&media)
	if result.Error != nil {
		return media, result.Error
	}
	return this.FindById(media.Id)
}

func (this *Play) Delete(dict Play) {
	Mysql.DB.Delete(&dict)
}

// 批量删除
func (this *Play) DeleteAll(where string) {
	Mysql.DB.Where(where).Delete(&Play{})
	return
}

func (this *Play) InsertAll(medias *[]Play) {
	Mysql.DB.Create(&*medias)
	return
}

// 批量删除
func (this *Play) DeleteByFilter(param *common.Filter) {
	db := Mysql.DB

	if param.Filter == nil {
		return
	}
	// 组装过滤条件

	for key, val := range param.Filter {
		vals := val.(map[string]interface{})
		if strings.Contains(key, "bw_") {
			db = db.Where(vals["condition"], vals["val1"], vals["val2"])
		} else {
			db = db.Where(vals["condition"], vals["val"])
		}
	}
	db.Delete(Play{})

}
